package renpyas3.view.components 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import renpyas3.model.structs.RenpyOptions;
	
	/**
	 * hud view
	 */
	public class Hud extends Sprite
	{
		public static const FINISHED_ANIMATING_TEXT_WRITING:String = "finishedAnimatingTextWriting";
		
		private static const INTERVAL_NEXT_LETTER_APPEARS:Number = 20;
		
		private var _hudText:TextField;		
		private var _charNameHtmlText:String;
		private var _textToAnimate:String;
		private var _textToAnimateIndex:uint;
		private var _showNextLetterInterval:uint;
		private var _htmlTagsIndices:Array;
		
		public function Hud(options:RenpyOptions) 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			//transparent box catching mouse clicks 
			graphics.beginFill(0x0000FF, 0);
			graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight); //(size of the stage)
			graphics.endFill();
			
			//overlay text box
			graphics.beginFill(0x000000, 0.7);
			graphics.drawRect(0, 450, this.stage.stageWidth, 150);
			graphics.endFill();
			
			_hudText = new TextField();
			_hudText.width = 620;
			_hudText.height = 130;
			_hudText.selectable = false;
			_hudText.multiline = true;
			_hudText.wordWrap = true;
			_hudText.x = 10;
			_hudText.y = 460;
			_hudText.defaultTextFormat = new TextFormat("Verdana", 24, 0xFFFFFF, false);
			//_hudText.text = "long test text that extends for two lines to try the multiline and wordwrap properties";
			addChild(_hudText)
			
		}
		
		/**
		 * animates text writing, by showing text letter by letter
		 */
		public function animateTextWriting(textToAnimate:String,charName:String="",charColor:String=""):void
		{
			if (charName == "")
			{
				_charNameHtmlText = "";
				_hudText.htmlText = "";
			}
			else
			{
				_charNameHtmlText = "<font color=\"#" + charColor + "\"><b>" + charName + "</b></font><br>";
			}
			_textToAnimate = "<font color=\"#FFFFFF\">" + textToAnimate + "</font>";
			_textToAnimateIndex = 0;
			
			//find all the html tags in the text
			var re1:RegExp = /<.*?>/g;
			var result:Object = re1.exec(textToAnimate);

			_htmlTagsIndices = new Array();
			while (result != null) 
			{
				_htmlTagsIndices[result.index] = result;
				
				//Logger.info( result.index, "\t", result);
				result = re1.exec(textToAnimate);
			}
			
			scheduleNextLetterAppear();
		}
		
		/**
		 * setTimeout function to use in two points of the code
		 */
		private function scheduleNextLetterAppear():void
		{
			if (_textToAnimateIndex < _textToAnimate.length)
			{
				_showNextLetterInterval = setTimeout(showNextLetter, INTERVAL_NEXT_LETTER_APPEARS);
			}
			else
			{
				//finished animating
				dispatchEvent(new Event(FINISHED_ANIMATING_TEXT_WRITING));
			}
		}
		
		/**
		 * show next letter in writing animation
		 */
		private function showNextLetter():void
		{
			_textToAnimateIndex++;

			//if there is an html tag in this index
			if (_htmlTagsIndices[_textToAnimateIndex] != undefined)
			{
				//add the whole tag as one letter, otherwise it will start adding the '<', '/' one by one
				_textToAnimateIndex += String(_htmlTagsIndices[_textToAnimateIndex]).length;
			}

			_hudText.htmlText = _charNameHtmlText + _textToAnimate.substr(0, _textToAnimateIndex)+getFollowingTags();

			scheduleNextLetterAppear();
		}

		/**
		 * concat all the tags after _textToAnimateIndex as a string
		 * Is a lazy way to close the open tags
		 */
		private function getFollowingTags():String
		{
			var outStr:String = "";
			for (var i:uint=_textToAnimateIndex; i <  _htmlTagsIndices.length ; i++ )
			{
				if (_htmlTagsIndices[i] != undefined)
				{				
					outStr += _htmlTagsIndices[i];
				}
			}
			return outStr;
		}

		/**
		 * stop anim and write full text
		 */
		public function abortTextWritingAnim():void
		{
			clearTimeout(_showNextLetterInterval);
			_hudText.htmlText = _textToAnimate;
		}
		
		/**
		 * set hud text (no animation)
		 */
		public function set text(value:String):void 
		{
			_hudText.htmlText = value;
		}
		
	}

}